<template>
	<view class="content">
		<view class="mix-tree-list">
			<block v-for="(item, index) in treeList" :key="index">
				<view 
					class="mix-tree-item"
					:style="[{
							paddingLeft: item.rank*20 + 'px',
							zIndex: item.rank*-1 +50
						}]"
					:class="{
							border: treeParams.border == true,
							show: item.show,
							last: item.lastRank,
							showchild: item.showChild,
							rank1: item.rank == 1
						}"
					@click.stop="treeItemTap(item, index)"
				>
					<image class="mix-tree-icon" :style="{width: item.lastRank && item.showChild ? '16upx' : item.showChild ? treeParams.currentIcon : '12upx',height: item.lastRank && item.showChild ? '12upx' : item.showChild ? treeParams.currentIcon : '16upx'}" :src="item.lastRank && item.showChild ? treeParams.currentIcon : item.showChild ? treeParams.currentIcon : treeParams.defaultIcon"></image>
					<view class="innet-content">
						
						<view class="tree-user">{{item.username}} <text v-if="item.name && item.name.length > 0" class="inner-state">{{item.name}}</text></view>
						<view class="tree-bottom">
							<view class="yeji">
								<view>{{i18n.yeji_}}: {{item.yeji?(item.yeji):'0'}} TB</view>
								<view>{{i18n.yejis_}}: {{item.yeji_th?(item.yeji_th):'0'}} TH</view>
							</view>
						</view>
					</view>
				</view>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			list: {
				type: Array,
				default(){
					return [];
				}
			},
			params: {
				type: Object,
				default(){
					return {}
				}
			}
		},
		data() {
			return {
				treeList: [],
				treeParams: {
					defaultIcon: '../../static/newImg/right.png',
					currentIcon: '../../static/newImg/bottom.png',
					lastIcon: '',
					border: false
				}
			}
		},
		computed: {
			i18n () {  
				return this.$t('message')
			}
		},
		watch: {
			list(list){
				this.treeParams = Object.assign(this.treeParams, this.params);
				this.renderTreeList(list);
			}
		},
		methods: {
			//扁平化树结构
			renderTreeList(list=[], rank=0, parentId=[]){
				list.forEach(item=>{
					this.treeList.push({
						id: item.id,
						username: item.username,
						name: item.name,
						freeze: item.freeze,
						yeji: item.yeji,
						yeji_th: item.yeji_th,
						parentId,  // 父级id数组
						rank,  // 层级
						showChild: false,  //子级是否显示
						show: rank == 0  // 自身是否显示
					})
					if(Array.isArray(item.xj) && item.xj.length > 0){
						let parents = [...parentId];
						parents.push(item.id);
						this.renderTreeList(item.xj, rank+1, parents);
					}else{
						this.treeList[this.treeList.length-1].lastRank = true;
					}
				})
			},
			// 点击
			treeItemTap(item){
				// if (item.rank == 2) return;
				if (item.rank == 0) return;
				let list = this.treeList;
				let id = item.id;
				// if(item.lastRank == true){
				// 	//点击最后一级时触发事件
				// 	this.$emit('treeItemClick', item);
				// 	return;
				// }
				item.showChild = !item.showChild;
				list.forEach(childItem=>{
					if(item.showChild == false){
						if(!childItem.parentId.includes(id)){
							return;
						}
						if(childItem.lastRank !== true){
							childItem.showChild = false;
						}
						childItem.show = false;
					}else{
						if(childItem.parentId[childItem.parentId.length-1] == id){
							childItem.show = true;
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		overflow: hidden;
	}
	.mix-tree-list{
		display: flex;
		flex-direction: column;
		padding-left: 30upx;
	}
	.mix-tree-item{
		display: flex;
		align-items: center;
		flex-direction: row;
		font-size: 30upx;
		color: #333;
		height: 0;
		opacity: 0;
		transition: .2s;
		position: relative;
		.innet-content {
			// display: flex;
			// justify-content: space-between;
			// flex-direction: column;
			// align-items: center;
			width: 100%;
			.tree-user {
				// flex: 1;
				color: #FFF;
				text {
					padding: 4upx 10upx;
					background-color: rgba(45, 170, 255, .2);
					color: rgba(45, 170, 255, 1);
					font-size: 9px;
					margin-left: 30upx;
					border-radius: 4upx;
					margin-top: -2px;
				}
			}
			.tree-bottom {
				// width: 200upx;
				// flex-shrink: 1;
				// display: flex;
				// flex-direction: row;
				// justify-content: space-between;
				// align-items: center;
				margin-right: 18upx;
				// width: 100%;
				font-size: 24upx;
				padding: 15upx 20upx;
				view {
					flex-shrink: 1;
					color: #FFF;
				}
				// view+view {
				// 	margin-left: 40upx;
				// }
			}
		}
	}
	.mix-tree-item.border{
		border-bottom: 1px solid #eee;
	}
	.mix-tree-item.show{
		min-height: 100upx;
		opacity: 1;
	}
	.mix-tree-item.show.rank1 {
		min-height: 100upx;
	}
	.mix-tree-icon{
		width: 26upx;
		height: 26upx;
		margin-right: 8upx;
		opacity: .9;
	}
	
	.mix-tree-item.showchild:before{
		transform: rotate(90deg);
	}
	.mix-tree-item.last:before{
		opacity: 0;
	}
</style>
